<?php
/**
 * 获取投注列表
 *
 *
 *
 * @copyright  Copyright (c) 2014-2015 TuntronKJ Inc. (http://www.tuntron.com)
 * @license    http://www.tuntron.com
 * @link       http://www.tuntron.com
 * @since      File available since Release v1.1
 */
defined('InTuntronKJ') or exit('Access Invalid!');
class GetBetOrderRecordControl extends BaseLotteryHallControl{

	public function indexOp(){
		
		$model_bets = Model('bat');
		$model_member = Model('member');
		$model_data = Model('data_class');
		$model_play = Model("played");
		$param['type'] = $_POST['lotteryId'];
		$param['uid'] = $_SESSION['uid'];
		
		$page = $_POST['page']?$_POST['page']:1;
		$start = 10*$page-10;
		
		$today_zero = strtotime('today');
		$time = time();
		$fromTime = $today_zero+3600*3;			//查询起始时间
		if($fromTime > $time){
			$toTime = $fromTime;				//查询结束时间
			$fromTime = $today_zero-3600*21;
		}else{
			$toTime = $fromTime+3600*24;
		}
		$param['actionTime'] = array('between',$fromTime.','.$toTime);
		$param['isDelete'] = 0;
		$member_info = $model_member->getMemberInfo(array('uid'=>$_SESSION['uid']));
		$bets_list = $model_bets->getBetList($param,'','',$start.',10','actionTime desc');		
		$bets_count = $model_bets->getCount($param);
		
		if(!empty($bets_list) && is_array($bets_list)){
			if($bets_count%10 == 0){
				$totalpage = $bets_count/10;
			}else{
				$totalpage = intval($bets_count/10)+1;
			}
			
			foreach($bets_list as $k => $v)
			{
				$playinfo = $model_play->getPlayInfo(array('id'=>$v['playedId']));
				
				$JSON_bets[$k]['RowNumber'] = $k+1;
				$JSON_bets[$k]['BetContent'] = $v['actionData'];
				$JSON_bets[$k]['BonusNumber'] = $v['lotteryNo'];
				$JSON_bets[$k]['InsertDate'] = date('Y-m-d H:i:s',$v['actionTime']);
				$JSON_bets[$k]['UserName'] = $v['username'];
				$JSON_bets[$k]['UserType'] = $member_info['type']==0?'会员':'代理';;
				$JSON_bets[$k]['RuleName'] = $playinfo['name'];
				$JSON_bets[$k]['LotteryName'] = "重庆彩";
				$JSON_bets[$k]['Id'] = $v['id'];
				$JSON_bets[$k]['Uid'] = $v['uid'];
				$JSON_bets[$k]['MemberId'] = $_SESSION['uid'];
				$JSON_bets[$k]['LotteryId'] = 1;
				$JSON_bets[$k]['IssueNumber'] = $v['actionNo'];
				$JSON_bets[$k]['BetRuleId'] = $v['playedId'];
				$JSON_bets[$k]['BetUnitPrice'] = $v['mode'];
				$JSON_bets[$k]['Description'] = "重庆彩 ".$v['actionNo']."期 ".$playinfo['name'];
				//是否开奖
				$kjdata = $model_data->getDataList(array('number'=>$v['actionNo'],'type'=>1));
				if($kjdata){
					$JSON_bets[$k]['hasRunLottery'] = "是";
				}else{
					$JSON_bets[$k]['hasRunLottery'] = "否";
				}
				$JSON_bets[$k]['UnitPaymentPrice'] = $v['bonusProp'];					//赔率
				$JSON_bets[$k]['ReturnPoint'] = $v['fanDian'];							//返点
				$JSON_bets[$k]['BonusMoney'] = $v['bonus'];
				$JSON_bets[$k]['EncodeId'] = $v['wjorderId'];
				$JSON_bets[$k]['OrderDate'] = "/Date(".$v['actionTime'].")/";
				$JSON_bets[$k]['OrderStatus'] = $v['isDelete']?10:0;
				$JSON_bets[$k]['OrderGuid'] = $v['serializeId'];
				$JSON_bets[$k]['TotalBet'] = $v['actionNum']*$v['beiShu'];
			}
		}
		//有亏情况
		foreach($bets_list as $k => $v)
		{
			$lotteryNumberDict[$typeid.",".$v['actionNo']]="";
		}

		$JSON = array(
			'totalPages'=>$totalpage,
			'totalRecords'=>$bets_count,
			'rows'=>$JSON_bets,
			'lotteryNumberDict'=>$lotteryNumberDict,
			);
		$output = json_encode($JSON);
		echo $output;
		exit();
	}

	public function postCodeOp(){


		$urlshang = $_SERVER['HTTP_REFERER']; //上一页URL     
		$urldan = $_SERVER['SERVER_NAME']; //本站域名
		$urlcheck=substr($urlshang,7,strlen($urldan));
		if($urlcheck<>$urldan){

			Re_json_encode('-100','投注不成功！原因:数据异常!');

		}

		if($_SESSION['uid']){
				$json = array();
				$amount=0; //总金额
				$bettingList =  htmlspecialchars_decode($_POST['bettingList']);

				$bettingListArr0 = json_decode($bettingList,true);
				$bettingListArr = $bettingListArr0[0];


				// if($this->settings['switchBuy']==0) throw new Exception('本平台已经停止购买！');
				// if($this->settings['switchDLBuy']==0 && $this->user['type'])  throw new Exception('代理不能买单！');
				 if(empty($bettingListArr['selNums'])) Re_json_encode('-100','请先选择号码再提交投注');

				 $type = intval($_POST['lotteryId']);
				 //检查时间 期数
				 $model_data		= Model('data_class');
				$stopTime=$model_data->getGameStopTime($type);  //封单时间
				$actionTime=$model_data->getGameActionTime($type);  //当期时间
				$actionNo=$model_data->getGameActionNo($type);  //当期期数

				// if($actionTime!=$para['kjTime'])  throw new Exception('投注失败：你投注第'.$para['actionNo'].'已过购买时间');
				// if($actionNo!=$para['actionNo'])  throw new Exception('投注失败：你投注第'.$para['actionNo'].'已过购买时间');
				// if($stopTime<$this->time) throw new Exception('投注失败：你投注第'.$para['actionNo'].'已过购买时间');

				$model_member	= Model('member');

				$member_info = $model_member->getMemberInfo(array('uid' =>$_SESSION['uid']),'coin');
					//查询可用金额
					$amount = $bettingListArr['totalBetting']*$bettingListArr['unitPrice'];
					if($member_info['coin'] < $amount){

						Re_json_encode('-100','投注不成功！原因:游戏分数不够本次投注,请先充值!');
					}
				$model_bat	= Model('bat');
				try {

			    $model_bat->beginTransaction();

			  	
				
			  echo ('{"infoList":[{"Key":"0,","Value":"[后三直选复式]投注成功"}],"useInfo":{"GamePoint":"10.00","AvailablePoint":"8.29","FreezePoint":"1.71"}}');
			  exit();
			} catch (Exception $e) {
			    $this->rollback();
	            return array('error' => $message);
			}

		}

		
		
	}

}
